<%@ taglib uri="/WEB-INF/tld/struts-html.tld" prefix="html"%>
<%@ taglib uri="/WEB-INF/tld/struts-logic.tld" prefix="logic"%>
<%@ taglib uri="/WEB-INF/tld/struts-bean.tld" prefix="bean"%>
<%@page import="java.util.*"%>
<%@page import="Filesharing.FileModel"%>
<%@page import="Eclass.Schedule.ScheduleModel"%>

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
	pageEncoding="ISO-8859-1"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Class Schedule</title>
<link rel="stylesheet" type="text/css" href="css/FinalProjectCSS.css"/>
<script type="text/javascript" src="js/MasterJS.js"></script>
<script type="text/javascript">
	function flyToPage(task, batchClassId, subjectTitle) {
		if(batchClassId != "") {
			document.forms[0].elements['modelFileSch.batchClassId'].value = batchClassId;
			document.forms[0].elements['modelSchedule.subjectTitle'].value = subjectTitle;
		}
		document.forms[0].task.value = task;
		document.forms[0].submit();
	}
	
	function flyToPage2(key) {
		document.forms[0].task.value = "goto";
		var currentPage = parseInt(document.forms[0].elements["currentPage"].value);
		var currentPage2 = currentPage;
		
// 		if(key == "") {
// 			var hal = parseInt(document.getElementById("noOfQuestion").value);
// 			if(hal>=1 && hal<= document.forms[0].elements["totalQuestion"].value)
// 			{
// 				//document.forms[0].task.value = "goto";
// 				currentPage2 = hal;
// 			} 
// 			else 
// 			{
// 				document.forms[0].task.value = "";
// 				document.getElementById("noOfQuestion").value = "";
// 				alert("No of page is not valid!");
// 			}
// 		} else 
		if(currentPage2 == 1 && key == "prev") {
			alert("You already in first page");
		} else if(currentPage2 == document.forms[0].elements["totalPage"].value && key == "next") {
			alert("You already in last page");
		} else {
			if(key == "prev") {
				currentPage2 = (currentPage - 1);
			} else if(key == "next") {
				currentPage2 = (currentPage + 1);
			}
		}
		
		document.forms[0].elements["currentPage"].value = currentPage2;
		
		document.forms[0].submit();
	}
	
	function downloadFileSubject(fileSubjectPathDl) {
		//alert("donlod " + fileSubjectPathDl);
		document.forms[0].task.value = "downloadFileSubject";
		document.forms[0].elements['modelFileSch.filePath'].value = fileSubjectPathDl;
		document.forms[0].submit();
	}
	
	function updateFileSubject(fileSubjectIdUpd, subjectTitle, uploaderIdUpd) {
		document.forms[0].task.value = "updateFileSubject";
		document.forms[0].elements['modelFileSch.fileId'].value = fileSubjectIdUpd;
		document.forms[0].elements['modelSchedule.subjectTitle'].value = subjectTitle;
		document.forms[0].elements['modelFileSch.uploaderId'].value = uploaderIdUpd;
		document.forms[0].submit();
	}
	
	function deleteFileSubject(fileSubjectIdDel, fileSubjectNameDel, uploaderIdDel) {
		 r = confirm("Delete " +fileSubjectNameDel+ " file subject?");
		 if(r == true) {
			 document.forms[0].task.value = "deleteFileSubject";
			 document.forms[0].elements['modelFileSch.fileId'].value = fileSubjectIdDel;
			 document.forms[0].elements['modelFileSch.fileName'].value = fileSubjectNameDel;
			 document.forms[0].elements['modelFileSch.uploaderId'].value = uploaderIdDel;
			 document.forms[0].submit();	
		 }
	}
	
	function openFile(path) {
		document.forms[0].task.value = "openFile";
		document.forms[0].pathOpenFile.value = path;
		alert(path);
		document.forms[0].submit();
	}
	
	function showFileNotFound(fileNameNotFound){
		alert("File " +fileNameNotFound+ " does not exist");
	}
</script>

<%
	if(session.getAttribute("result") != null)
	{
		String message = session.getAttribute("result").toString();
		%>
			<script type="text/javascript">
				window.alert('<%=message%>');
			</script>
		<%
			session.removeAttribute("result");
	}
%> 

</head>
<body onkeydown="javascript:disableF5(event, 'listSchedule');">
	<center>
		<table width="100%" class="bodyTable boxShadow">
		
			<!-- Header -->
			<tr>
				<td colspan="2"><%@include file="/include/Header.jsp"%></td>
			</tr>
		
			<!-- Content -->
			<tr>
				<td rowspan="2" width="200px" class="navigationBox"><%@include file="/include/Navigation.jsp"%></td>
				<td align="left" class="headerBox blueBackground">
					<% if("admin".equalsIgnoreCase(user.getRole())) { %>
					ACE Management >
					<% } %>
					Class Schedule
				</td>
			</tr>
			<tr>
				<td align="center" class="contentStyle"><br>
					
					<!-- Insert Code Here -->
					<html:form action="/Schedule" method="post">
						<html:hidden property="role"/>
						<html:hidden property="task" name="scheduleForm" />
						<html:hidden property="modelFileSch.fileId" name="scheduleForm" />
						<html:hidden property="modelFileSch.fileName" name="scheduleForm" />
						<html:hidden property="modelFileSch.filePath" name="scheduleForm" />
						<html:hidden property="modelFileSch.uploaderId" name="scheduleForm" />
						<html:hidden property="modelFileSch.batchClassId" name="scheduleForm" />
						<html:hidden property="modelSchedule.subjectTitle" name="scheduleForm" />
						<html:hidden property="pathOpenFile" name="scheduleForm"/>
						<html:hidden property="totalPage" name="scheduleForm"/>
						<html:hidden property="currentPage" name="scheduleForm"/>
						
						<logic:notEqual value="trainee" property="role" name="scheduleForm">
						<table class="boxShadow" width="95%" border="0">
			    			<tr class="headerBox greyBackground">
			    				<td width="90%"></td>
			    				<td class="formListStyle" align="right">Batch</td>
			    				<td class="formListStyle" align="right">:</td>
			    				<td class="formListStyle" align="right">
									<html:select property="selectedBatchId" onchange="javascript:flyToPage('listSchedule','','')">
										<logic:notEmpty name="scheduleForm" property="listBatch">
											<html:optionsCollection name="scheduleForm" property="listBatch" label="batch" value="batchId"/>
										</logic:notEmpty>
									</html:select>
			    				</td>
			    			</tr>
		    			</table>
		    			</logic:notEqual>
		    			<br>
						
						<table align="center" width="95%" border="0" class="boxShadow">
							<tr class="headerBox formHeaderBackground">
								<th colspan="2" style="width:20%;" class="formListStyle">Subject's Title</th>
								<th class="formListStyle">Start Date</th>
								<th class="formListStyle">End Date</th>
								<th class="formListStyle">Teacher's Name</th>
								<th class="formListStyle">Assistant's Name</th>
								<th class="formListStyle">&nbsp;</th>
							</tr>
							<%  
								ArrayList listSch = (ArrayList)request.getAttribute("listSchedule");
								/*int size1 = listSch.size();
								for(int a=0;a<size1;a++){
									ScheduleModel sm = (ScheduleModel)listSch.get(a);
									System.out.println("** Subject: "+sm.getSubjectTitle());
									List lstFile = sm.getListFileSch();
									int size2=lstFile.size();
									for(int b=0;b<size2;b++){
										System.out.println("--- "+(FileModel) lstFile.get(b));
									}
								}*/
								//session.removeAttribute("listSchedule");
								int i=0;
							%>
							<logic:empty property="listSchedule" name="scheduleForm">
								<tr class="formListBackground">
									<td align="center" class="formListStyle" colspan="7">Data is not found</td>
								</tr>
							</logic:empty>
							<logic:notEmpty property="listSchedule" name="scheduleForm">
 								<logic:iterate name="scheduleForm" id="listData" property="listSchedule">
 									<tr class="formListBackground">
 										<td colspan="2" align="center" class="formListStyle"><bean:write name="listData" property="subjectTitle" /></td>
 										<td align="center" class="formListStyle"><bean:write name="listData" property="startDate" /></td>
 										<td align="center" class="formListStyle"><bean:write name="listData" property="endDate" /></td>
 										<td align="center" class="formListStyle"><bean:write name="listData" property="tchName" /></td>
 										<td align="center" class="formListStyle"><bean:write name="listData" property="astName" /></td>
 										<logic:equal value="admin" property="role" name="scheduleForm">
 											<td class="formListStyle" align="center">
 												<img src="images/uploadButton.png" title="upload" style="cursor: pointer;" 
 												onclick="javascript:flyToPage('insertFileSubject', '<bean:write name="listData" property="batchClassId" />', '<bean:write name="listData" property="subjectTitle" />');"/>
 											</td>
 										</logic:equal>
 										<logic:equal value="trainer" property="role" name="scheduleForm">
 											<td class="formListStyle" align="center">
 												<img src="images/uploadButton.png" title="upload" style="cursor: pointer;" 
 												onclick="javascript:flyToPage('insertFileSubject', '<bean:write name="listData" property="batchClassId" />', '<bean:write name="listData" property="subjectTitle" />');"/>
 											</td>
 										</logic:equal>
 										<logic:equal value="trainee" property="role" name="scheduleForm">
 											<td class="formListStyle">&nbsp;</td>
 										</logic:equal>
 									</tr>
 									
 									<tr>
 										<td colspan="7">
	 										<table border="0" style="border: 0px none;">
												
												<% 
			 										ScheduleModel sm = (ScheduleModel) listSch.get(i);
													List tmpFileListSch =  sm.getListFileSch();
													for (int j = 0; j < tmpFileListSch.size(); j++) {
														
														FileModel tmpListFile = (FileModel)tmpFileListSch.get(j);
														String tmpFileName = tmpListFile.getFileName();
														boolean cekFileName = false;
														if (tmpFileName.contains(".pptx") || tmpFileName.contains(".docx") || tmpFileName.contains(".xlsx") || 
																tmpFileName.contains(".ppt") || tmpFileName.contains(".doc") || tmpFileName.contains(".xls") || 
																tmpFileName.contains(".pdf")) {
															cekFileName = true;
														}
			 									%>
												
	 											<tr>
	 												<td width="100px"></td>
	 												<td class="formListStyle" width="750px" align="left">
	 													&nbsp;&nbsp;&nbsp;&nbsp;&diams; <%= ((FileModel)tmpFileListSch.get(j)).getFileName() %>
	 													
	 													<% if(cekFileName) { 
	 														if(tmpListFile.getIsFileExist()) { %>
														
															<a href="javascript:void(0);" onclick="javascript:openFile('<%= ((FileModel)tmpFileListSch.get(j)).getFileName() %>');">
																<img src="images/fileviewer.png" width="17px" height="17px" />
															</a>
															
															<% } else { %>
																<a href="javascript:void(0)" onclick="javascript:showFileNotFound('<%= ((FileModel)tmpFileListSch.get(j)).getFileName() %>');">
																	<img src="images/fileviewer.png" width="17px" height="17px" />
																</a>
															<% } %>
														<% } %>
														
	 												</td>
	 												<logic:equal value="admin" property="role" name="scheduleForm">
			 											<td class="formListStyle" width="120px" align="center">
			 												<% if(tmpListFile.getIsFileExist()) {%>
				 												<a href="<%= ((FileModel)tmpFileListSch.get(j)).getFilePath() %><%= ((FileModel)tmpFileListSch.get(j)).getFileName() %>" download>
				 													<img src="images/downloadButton.png" title="download" style="cursor: pointer;"/>
				 												</a> &nbsp;
				 											<% } else { %>
																<a href="javascript:void(0);" onclick="javascript:showFileNotFound('<%= ((FileModel)tmpFileListSch.get(j)).getFileName() %>');">
																	<img src="images/downloadButton.png" title="download" style="cursor: pointer;"/>
																</a>&nbsp;
															<% } %>
			 												<img src="images/editButton.png" title="edit" style="cursor: pointer;" 
			 													onclick="javascript:updateFileSubject('<%= ((FileModel)tmpFileListSch.get(j)).getFileId() %>', 
			 													'<%= sm.getSubjectTitle() %>', '<%= ((FileModel)tmpFileListSch.get(j)).getUploaderId() %>');"/> &nbsp;
	 														<img src="images/deleteButton.png" title="delete" style="cursor: pointer;" 
	 															onclick="javascript:deleteFileSubject('<%= ((FileModel)tmpFileListSch.get(j)).getFileId() %>', '<%= ((FileModel)tmpFileListSch.get(j)).getFileName() %>', 
	 															'<%= ((FileModel)tmpFileListSch.get(j)).getUploaderId() %>');"/>
	 													</td>
			 										</logic:equal>
			 										<logic:equal value="trainer" property="role" name="scheduleForm">
			 											<td class="formListStyle">
			 												<% if(tmpListFile.getIsFileExist()) {%>
				 												<a href="<%= ((FileModel)tmpFileListSch.get(j)).getFilePath() %><%= ((FileModel)tmpFileListSch.get(j)).getFileName() %>" download>
				 													<img src="images/downloadButton.png" title="download" style="cursor: pointer;"/>
				 												</a> &nbsp;
				 											<% } else { %>
																<a href="javascript:void(0);" onclick="javascript:showFileNotFound('<%= ((FileModel)tmpFileListSch.get(j)).getFileName() %>');">
																	<img src="images/downloadButton.png" title="download" style="cursor: pointer;"/>
																</a>&nbsp;
															<% } %>
			 												<img src="images/editButton.png" title="edit" style="cursor: pointer;" 
			 													onclick="javascript:updateFileSubject('<%= ((FileModel)tmpFileListSch.get(j)).getFileId() %>', 
			 													'<%= sm.getSubjectTitle() %>', '<%= ((FileModel)tmpFileListSch.get(j)).getUploaderId() %>');"/>
			 											</td>
			 										</logic:equal>
			 										<logic:equal value="trainee" property="role" name="scheduleForm">
			 											<td class="formListStyle">&nbsp;&nbsp;
			 												<% if(tmpListFile.getIsFileExist()) {%>
				 												<a href="<%= ((FileModel)tmpFileListSch.get(j)).getFilePath() %><%= ((FileModel)tmpFileListSch.get(j)).getFileName() %>" download>
				 													<img src="images/downloadButton.png" title="download" style="cursor: pointer;"/>
				 												</a>
				 											<% } else { %>
																<a href="javascript:void(0);" onclick="javascript:showFileNotFound('<%= ((FileModel)tmpFileListSch.get(j)).getFileName() %>');">
																	<img src="images/downloadButton.png" title="download" style="cursor: pointer;"/>
																</a>&nbsp;
															<% } %>
			 											</td>
			 										</logic:equal>
	 											</tr>
	 											<% } i++; %>
	 											
	 										</table>
 										</td>
 									</tr>
 								</logic:iterate>
							</logic:notEmpty>
							<tr class="headerBox formHeaderBackground">
								<td class="formListStyle" width="10%">
									<a href="javascript:flyToPage2('prev')">&lt;&lt; Previous</a>
								</td> 
								<td class="formListStyle" colspan="5" align="center">
									Page <bean:write property="currentPage" name="scheduleForm"/> of <bean:write property="totalPage" name="scheduleForm"/>
								</td> 
								<td class="formListStyle" align="right" width="10%">
									<a href="javascript:flyToPage2('next')">Next &gt;&gt;</a>
								</td> 
							</tr>	 						
						</table>
					</html:form>
					
					<br><br>
				</td>
			</tr>
			
			<!-- Footer -->
			<tr>
				<td align="center" colspan="2" class="headerBox blueBackground">&copy; OME. 2014. All Rights Reserved.</td>
			</tr>
		</table>
	</center>
</body>
</html>